<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar fixed title="文章详情" left-arrow @click-left="$router.back()" />
    <van-loading type="spinner" color="#1989fa" vertical v-if="loading">飞速加载中...</van-loading>
    <!-- 文章信息区域 -->
    <div class="article-container" v-else>
      <!-- 文章标题 -->
      <h1 class="art-title">{{ artObj.title }}</h1>

      <!-- 用户信息 -->
      <van-cell
        center
        :title="
          artObj.aut_name === '黑马程序员(改不了)' || artObj.aut_name === '黑马先锋'
            ? 'Hero'
            : artObj.aut_name
        "
        :label="formatDate(artObj.pubdate)"
      >
        <template #icon>
          <img :src="artObj.aut_photo" alt="" class="avatar" />
        </template>
        <template #default>
          <div @click="followClickFn()">
            <van-button type="info" size="mini" v-if="artObj.is_followed">已关注</van-button>
            <van-button icon="plus" type="info" size="mini" plain v-else>关注</van-button>
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="artObj.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box" @click="likeClickFn(artObj.attitude === 1)">
        <!-- -1 无态度, 0 未点赞, 1 点赞, 是当前登录用户对此文章的态度 -->
        <van-button icon="good-job" type="danger" size="small" v-if="artObj.attitude === 1"
          >已点赞</van-button
        >
        <van-button icon="good-job-o" type="danger" plain size="small" v-else>点赞</van-button>
      </div>
      <!-- 评论 -->
      <div class="comment">
        <CommentList :collectedArt="isCollected" @changeCollect="changeCollectFn"></CommentList>
      </div>
    </div>
  </div>
</template>

<script>
import {
  articleDetailAPI,
  userFollowAPI,
  userUnFollowAPI,
  artLikeAPI,
  artDislikeAPI
} from '@/api/index.js'
import { timeAgo } from '@/utils/day.js'
import CommentList from '@/views/ArticleDetail/CommentList.vue'
export default {
  name: 'Detail',
  data() {
    return {
      artObj: {}, // 文章对象
      isCollected: false, // 是否收藏文章
      loading: true // 是否正在加载
    }
  },
  async created() {
    const {
      data: { data }
    } = await articleDetailAPI(this.$route.query)
    console.log(data)
    this.artObj = data
    this.isCollected = data.is_collected
    this.loading = false
  },
  methods: {
    // 重置时间
    formatDate: timeAgo,
    // 点击关注按钮
    followClickFn() {
      if (this.artObj.is_followed === true) {
        // 点击 已关注
        // 业务->取消关注
        this.artObj.is_followed = false
        userUnFollowAPI(this.artObj)
      } else {
        // 点击 关注
        // 业务->关注作者
        this.artObj.is_followed = true
        userFollowAPI(this.artObj)
      }
    },
    // 点赞按钮
    likeClickFn(bool) {
      if (bool === true) {
        // 点击了 已点赞
        // 业务->取消点赞
        this.artObj.attitude = 0
        artDislikeAPI(this.artObj)
      } else {
        // 点击了 点赞
        // 业务 -> 点赞文章
        this.artObj.attitude = 1
        artLikeAPI(this.artObj)
      }
    },
    // 改变收藏状态
    changeCollectFn() {
      this.isCollected = !this.isCollected
    }
  },
  components: {
    CommentList
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}

.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img {
    width: 100%;
  }
  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
.van-loading {
  margin-top: 60px;
}
</style>
